using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class PropManager : Singleton_Base
{
    public ObjectGroup<GameObject, string> PropInteractorPair;

    List<Prop> propList;

    void Start(){
        propList = new List<Prop>();
        Debug.Log(propList.Count);
    }

    void OnEnable(){
        EventHandler_Prop.GetPropEvent += OnGetPropEvent;
    }

    void OnDisable(){
        EventHandler_Prop.GetPropEvent -= OnGetPropEvent;
    }

    void OnTriggerEnter2D(Collider2D other){
        if(PropInteractorPair.Contains(other.gameObject)){
            string needProp = PropInteractorPair[other.gameObject];
            GameObject needPropObject = CheckNeedProp(needProp);
            if(needPropObject != null){
                other.gameObject.GetComponent<PropClass>().OnPropTriggerEvent(needPropObject);
            }
        }
    }

    GameObject CheckNeedProp(string tag){
        foreach(Prop prop in propList){
            Debug.Log(prop);
            if(prop.propTag == tag){
                return prop.propObject;
            }
        }
        return null;
    }

    class Prop{
        public GameObject propObject;
        public string propTag;

        public Prop(GameObject obj, string tag){
            this.propObject = obj;
            this.propTag = tag;
        }
    }

    void OnGetPropEvent(GameObject obj){
        propList.Add(new Prop(
            obj,
            obj.GetComponent<PropClass>().propTag)
        );
    }

}
